package com.dongdong.service.impl;

import com.dongdong.api.ItemApi;
import com.dongdong.model.TbItem;
import com.dongdong.service.CartService;
import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.CartDetailVO;
import com.dongdong.vo.CartVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private ItemApi itemApi;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void addCart(String accessToken, Long skuId, Integer num) {
        String userId = jwtUtil.parseJwt(accessToken).getId();
//        根据skuId返回商品对象
        TbItem item = itemApi.getItemById(skuId);
//      从redis中获取当前用户的购物车集合
        List<CartVO> cartVOS = getRedisCart(userId);
//        如果集合中没有当前商家
        CartVO cart = null;
        for (CartVO cartVO : cartVOS) {
            if (Objects.equals(cartVO.getSeller(),item.getSeller())){
                cart = cartVO;
                break;
            }

        }

        if (cart == null){
//            没有当前商家，添加商家和明细
            cart = new CartVO();
            cart.setSeller(item.getSeller());
            List<CartDetailVO> detailVOS = new ArrayList<>();
            CartDetailVO detailVO = new CartDetailVO();
            detailVO.setNum(num);
            detailVO.setTitle(item.getTitle());
            detailVO.setPrice(item.getPrice());
            detailVO.setPicPath(item.getImage());
            detailVO.setSellerId(item.getSellerId());
            detailVO.setTotalFee(item.getPrice().multiply(new BigDecimal(num)));
            detailVO.setGoodsId(item.getGoodsId());
            detailVO.setItemId(skuId);

            detailVOS.add(detailVO);
            cart.setDetailVOS(detailVOS);
            cartVOS.add(cart);
        }else {
            //        不为空，添加明细
            List<CartDetailVO> detailVOS = cart.getDetailVOS();
            CartDetailVO detail = null;
            for (CartDetailVO detailVO : detailVOS) {
                if (Objects.equals(detailVO.getItemId(),skuId)){
                    detail = detailVO;
                    break;
                }
            }
            if (detail== null){
//        判断  是否有同类商品
//        没有 添加明细
                detail = new CartDetailVO();
                detail.setNum(num);
                detail.setTitle(item.getTitle());
                detail.setPrice(item.getPrice());
                detail.setPicPath(item.getImage());
                detail.setSellerId(item.getSellerId());
                detail.setTotalFee(item.getPrice().multiply(new BigDecimal(num)));
                detail.setGoodsId(item.getGoodsId());
                detail.setItemId(skuId);
                detailVOS.add(detail);
            }else {
//        有,增加数量 和 小计
                detail.setNum(detail.getNum()+num);
                detail.setTotalFee(item.getPrice().multiply(new BigDecimal(detail.getNum())));

                //        判断数量和商家
                if (detail.getNum()<=0){
                    detailVOS.remove(detail);
                }
                if (detailVOS.size()<=0){
                    cartVOS.remove(cart);
                }
            }
        }
//        存到redis中
        putRedisCart(userId,cartVOS);
    }

    @Override
    public List<CartVO> getCart(String accessToken) {
        Claims claims = jwtUtil.parseJwt(accessToken);
        String userId = claims.getId();
        List<CartVO> cartVOS = getRedisCart(userId);
        return cartVOS;
    }

    private void putRedisCart(String userId, List<CartVO> cartVOS) {

        redisTemplate.boundHashOps("cart").put(userId,cartVOS);
    }

    private List<CartVO> getRedisCart(String userId) {

        List<CartVO> list = (List<CartVO>) redisTemplate.boundHashOps("cart").get(userId);
        if (list == null){
            list = new ArrayList<>();
        }
        return list;
    }


}
